﻿using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using FranceTriple.Dal;

namespace TeamFranceTriple
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        public Requester Requester { get; set; }
        private HtmlLink newStyleSheet, newStyleSheet2;
        private HtmlMeta metaDescription, metaKeywords, metaAuthor, metaGenerator;

        private MenuItem HomeLink1mi, ForumLink1mi, EventLink1mi, PictureLink1mi, FriendlyTeamLink1mi, MemberLink1mi, AboutLink1mi, FanLink1mi;

        protected SiteMaster()
        {

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //Todo : invalid session state lorsque on passe en modification sur une ligne de la grille des messages
            //NoBotState state;
            //if (!NoBot1.IsValid(out state))
            //{
            //    //Solved : Incident "BadSession" récurrents qui apparaissent pendant l'utilisation normale de l'application.
            //    //Solution : Set ViewStateMode="Enabled" on .aspx pages.
            //    Label1.Text = "Data refused (" + state.ToString().TextToHtml() + ")"; 
            //} 

            HomeLink1mi = new MenuItem() { NavigateUrl = String.Format("~/{0}/home/", Requester.Culture), ToolTip = "HomeLink1Resource1_ToolTip".ToLocalizedString(), ImageUrl = "http://img15.imageshack.us/img15/3414/portailw.png" };
            Menu1.Items.Add(HomeLink1mi);

            ForumLink1mi = new MenuItem() { NavigateUrl = String.Format("~/{0}/forum/", Requester.Culture), ToolTip = "ForumLink1Resource1_ToolTip".ToLocalizedString(), ImageUrl = "http://img842.imageshack.us/img842/7282/forumnzg.png" };
            Menu1.Items.Add(ForumLink1mi);

            FriendlyTeamLink1mi = new MenuItem() { Target = "_Blank", NavigateUrl = String.Format("~/{0}/labaff/?labaffteampagenumber=1", Requester.Culture), ToolTip = "FriendlyTeamResource1_ToolTip".ToLocalizedString(), ImageUrl = "http://img24.imageshack.us/img24/7450/labaff.png" };
            Menu1.Items.Add(FriendlyTeamLink1mi);

            MemberLink1mi = new MenuItem() { NavigateUrl = String.Format("~/{0}/memberlist/", Requester.Culture), ToolTip = "MemberLink1Resource1_ToolTip".ToLocalizedString(), ImageUrl = "http://img248.imageshack.us/img248/6593/ourmembers.png" };
            Menu1.Items.Add(MemberLink1mi);

            FanLink1mi = new MenuItem() { NavigateUrl = String.Format("~/{0}/fanlist/", Requester.Culture), ToolTip = "FanResource1_ToolTip".ToLocalizedString(), ImageUrl = "http://img694.imageshack.us/img694/6419/ourfans.png" };
            Menu1.Items.Add(FanLink1mi);

            if (Requester.Roles.Contains("Member"))
            {
                EventLink1mi = new MenuItem() { NavigateUrl = String.Format("~/{0}/eventlist/", Requester.Culture), ToolTip = "EventLink1Resource1_ToolTip".ToLocalizedString(), ImageUrl = "http://img17.imageshack.us/img17/5646/eventsja.png" };
                Menu1.Items.Add(EventLink1mi);

                PictureLink1mi = new MenuItem() { NavigateUrl = String.Format("~/{0}/picturelist/", Requester.Culture), ToolTip = "PictureLink1Resource1_ToolTip".ToLocalizedString(), ImageUrl = "http://img855.imageshack.us/img855/8109/picturesj.png" };
                Menu1.Items.Add(PictureLink1mi);
            }

            AboutLink1mi = new MenuItem() { NavigateUrl = String.Format("~/{0}/about/", Requester.Culture), ToolTip = "AboutLink1Resource1_ToolTip".ToLocalizedString(), ImageUrl = "http://img545.imageshack.us/img545/499/aboutusv.png" };
            Menu1.Items.Add(AboutLink1mi);

            //HomeLink1mi = new MenuItem() { NavigateUrl = String.Format("~/{0}/home/", Requester.Culture), Text = "HomeLink1Resource1_Text".ToLocalizedString(), ToolTip = "HomeLink1Resource1_ToolTip".ToLocalizedString() };
            //Menu1.Items.Add(HomeLink1mi);

            //ForumLink1mi = new MenuItem() { NavigateUrl = String.Format("~/{0}/forum/", Requester.Culture), Text = "ForumLink1Resource1_Text".ToLocalizedString(), ToolTip = "ForumLink1Resource1_ToolTip".ToLocalizedString() };
            //Menu1.Items.Add(ForumLink1mi);

            //FriendlyTeamLink1mi = new MenuItem() { Target = "_Blank", NavigateUrl = String.Format("~/{0}/labaff/?labaffteampagenumber=1", Requester.Culture), Text = "FriendlyTeamResource1_Text".ToLocalizedString(), ToolTip = "FriendlyTeamResource1_ToolTip".ToLocalizedString() };
            //Menu1.Items.Add(FriendlyTeamLink1mi);

            //MemberLink1mi = new MenuItem() { NavigateUrl = String.Format("~/{0}/memberlist/", Requester.Culture), Text = "MemberLink1Resource1_Text".ToLocalizedString(), ToolTip = "MemberLink1Resource1_ToolTip".ToLocalizedString() };
            //Menu1.Items.Add(MemberLink1mi);

            //FanLink1mi = new MenuItem() { NavigateUrl = String.Format("~/{0}/fanlist/", Requester.Culture), Text = "FanResource1_Text".ToLocalizedString(), ToolTip = "FanResource1_ToolTip".ToLocalizedString() };
            //Menu1.Items.Add(FanLink1mi);

            //if (Requester.Roles.Contains("Member"))
            //{
            //    EventLink1mi = new MenuItem() { NavigateUrl = String.Format("~/{0}/eventlist/", Requester.Culture), Text = "EventLink1Resource1_Text".ToLocalizedString(), ToolTip = "EventLink1Resource1_ToolTip".ToLocalizedString() };
            //    Menu1.Items.Add(EventLink1mi);

            //    PictureLink1mi = new MenuItem() { NavigateUrl = String.Format("~/{0}/picturelist/", Requester.Culture), Text = "PictureLink1Resource1_Text".ToLocalizedString(), ToolTip = "PictureLink1Resource1_ToolTip".ToLocalizedString() };
            //    Menu1.Items.Add(PictureLink1mi);
            //}

            //AboutLink1mi = new MenuItem() { NavigateUrl = String.Format("~/{0}/about/", Requester.Culture), Text = "AboutLink1Resource1_Text".ToLocalizedString(), ToolTip = "AboutLink1Resource1_ToolTip".ToLocalizedString() };
            //Menu1.Items.Add(AboutLink1mi);

            FrenchLanguageHyperLink.NavigateUrl = Request.RawUrl.ToLower().Replace("en-us", "fr-fr");
            EnglishLanguageHyperLink.NavigateUrl = Request.RawUrl.ToLower().Replace("fr-fr", "en-us");

            SignupLink.NavigateUrl = String.Format("~/{0}/register/", Requester.Culture);
            SignupLink2.NavigateUrl = SignupLink.NavigateUrl;

            if (Session["AnonymousID"] != null)
            {
                SignupLink.Visible = true;
                SignupLink2.Visible = true;
            }
            else
            {
                SignupLink.Visible = false;
                SignupLink2.Visible = false;
            }

            metaDescription = new HtmlMeta();
            metaDescription.Name = "DESCRIPTION";
            metaDescription.Content = "MetaDescription".ToLocalizedString().TextToHtml();
            Page.Header.Controls.Add(metaDescription);

            metaKeywords = new HtmlMeta();
            metaKeywords.Name = "KEYWORDS";
            metaKeywords.Content = "metaKeywords".ToLocalizedString().TextToHtml();
            Page.Header.Controls.Add(metaKeywords);

            metaAuthor = new HtmlMeta();
            metaAuthor.Name = "AUTHOR";
            metaAuthor.Content = "metaAuthor".ToLocalizedString().TextToHtml();
            Page.Header.Controls.Add(metaAuthor);

            metaGenerator = new HtmlMeta();
            metaGenerator.Name = "GENERATOR";
            metaGenerator.Content = "metaGenerator".ToLocalizedString().TextToHtml();
            Page.Header.Controls.Add(metaGenerator);

            // Ajouter la feuille de style commune
            newStyleSheet = new HtmlLink();
            newStyleSheet.Href = "~/Styles/Site.css";
            newStyleSheet.Attributes.Add("type", "text/css");
            newStyleSheet.Attributes.Add("rel", "stylesheet");
            Page.Header.Controls.Add(newStyleSheet);

            // Selectionner automatiquement une feuille de style sans fond d'écran pour les mobiles et avec pour les autres
            newStyleSheet2 = new HtmlLink();
            if (Request.Browser.IsMobileDevice || Request.ServerVariables["HTTP_USER_AGENT"].ToString().ToLower().Contains("midori"))
            {
                newStyleSheet2.Href = "~/Styles/Site_Mobile.css";
            }
            else
            {
                newStyleSheet2.Href = "~/Styles/Site_Pc.css";
            }
            newStyleSheet2.Attributes.Add("type", "text/css");
            newStyleSheet2.Attributes.Add("rel", "stylesheet");
            Page.Header.Controls.Add(newStyleSheet2);

            Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate);
        }

        //protected void CustomChallengeResponse(object sender, AjaxControlToolkit.NoBotEventArgs e)
        //{
        //    Panel p = new Panel();
        //    p.ID = "NoBotPanel";
        //    Random rand = new Random();
        //    p.Width = rand.Next(300);
        //    p.Height = rand.Next(200);
        //    p.Style.Add(HtmlTextWriterStyle.Visibility, "hidden");
        //    p.Style.Add(HtmlTextWriterStyle.Position, "absolute");
        //    ((AjaxControlToolkit.NoBot)sender).Controls.Add(p);
        //    e.ChallengeScript = string.Format("var e = document.getElementById('{0}'); e.offsetWidth * e.offsetHeight;", p.ClientID);
        //    e.RequiredResponse = (p.Width.Value * p.Height.Value).ToString();
        //}
    }
}
